package cn.lzg.server;

import com.google.protobuf.GeneratedMessage;

import cn.lzg.common.core.model.Request;
import cn.lzg.common.core.model.Response;
import cn.lzg.common.core.model.Result;
import cn.lzg.common.core.model.ResultCode;
import cn.lzg.common.core.serial.Serializer;
import cn.lzg.common.core.session.Session;
import cn.lzg.common.core.session.SessionImpl;
import cn.lzg.common.core.session.SessionManager;
import cn.lzg.common.module.ModuleId;
import cn.lzg.server.module.user.entity.User;
import cn.lzg.server.scanner.Invoker;
import cn.lzg.server.scanner.InvokerHoler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

/**
 * 消息处理类
 * 
 * @author LizG
 *
 */
public class ServerHandler extends SimpleChannelInboundHandler<Request> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Request request) throws Exception {
		System.out.println("消息过来");
		handlerMessage(new SessionImpl(ctx.channel()), request);
	}

	/**
	 * 处理消息
	 * 
	 * @param session
	 * @param request
	 */
	protected void handlerMessage(Session session, Request request) {
		Response response = new Response(request);
		System.out.println("module:" + request.getModule());
		System.out.println("cmd:" + request.getCmd());
		// 获取命令执行器
		Invoker invoker = InvokerHoler.getInvoker(request.getModule(), request.getCmd());
		if (invoker != null) {
			Result<?> result = null;

			if (request.getModule() == ModuleId.USER) {
				result = (Result<?>) invoker.invoker(session, request.getData());
				System.out.println(result);
			} else if (request.getModule() == ModuleId.CHAT) {
				// 获取绑定的userid
				User user = (User) session.getAttachment();
				if (user != null) {
					result = (Result<?>) invoker.invoker(user.getUserId(), request.getData());
				} else {
					// 错误请求
					response.setStateCode(ResultCode.NO_HAS_LOGIN);
					session.write(response);
					return;
				}
			}

			// 处理逻辑返回,判断请求是否成功
			if (result.getResultCode() == ResultCode.SUCCESS) {
				// 回写数据给客户端
				Object object = result.getContent();
				if (object != null) {
					if (object instanceof Serializer) {
						Serializer content = (Serializer) object;
						byte[] bytes = content.getBytes();
						response.setData(bytes);
					} else if (object instanceof GeneratedMessage) {
						GeneratedMessage content = (GeneratedMessage) object;
						response.setData(content.toByteArray());
					} else {
						System.out.println(String.format("不可识别传输对象:%s", object));
					}
				}

				session.write(response);
			} else {
				response.setStateCode(result.getResultCode());
				session.write(response);
				return;
			}
		}else {
			response.setStateCode(ResultCode.NO_INVOKER);
			session.write(response);
			return;
		}
	}
	
	/**
	 *channel失效处理,客户端下线或者强制退出等任何情况都触发这个方法
	 */
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		Session session = new SessionImpl(ctx.channel());
		if(session!=null) {
			Object attachment = session.getAttachment();
			if(attachment!=null) {
				SessionManager.removeSession(((User)attachment).getUserId());
			}
		}
	}
}
